/**
 * @author goufahui 1344116821@qq.com
 * @version 0.0.3
 * @description tags多标签页
 */
const state = () => ({
  visitedRoutes: [],
})
const getters = {
  visitedRoutes: (state) => state.visitedRoutes,
}
const mutations = {
  //添加标签
  addVisitedRoute(state, route) {
    if (route) {
      let target = state.visitedRoutes.find((item) => item.path === route.path)
      if (target) {
        if (route.path !== target.path) Object.assign(target, route)
        return
      }
      state.visitedRoutes.push(Object.assign({}, route))
    } else {
      state.visitedRoutes = []
    }
  },
  //删除当前标签
  delVisitedRoute(state, route) {
    state.visitedRoutes.forEach((item, index) => {
      if (item.path === route.path) state.visitedRoutes.splice(index, 1)
    })
  },
  //删除当前标签以外其它全部多标签
  delOthersVisitedRoutes(state, route) {
    state.visitedRoutes = state.visitedRoutes.filter(
      (item) => item.affix || item.path === route.path
    )
  },
  //删除当前标签左边全部标签
  delLeftVisitedRoutes(state, route) {
    let index = state.visitedRoutes.length
    state.visitedRoutes = state.visitedRoutes.filter((item) => {
      if (item.name === route.name) index = state.visitedRoutes.indexOf(item)
      return item.affix || index <= state.visitedRoutes.indexOf(item)
    })
  },
  //删除当前标签右边全部标签
  delRightVisitedRoutes(state, route) {
    let index = state.visitedRoutes.length
    state.visitedRoutes = state.visitedRoutes.filter((item) => {
      if (item.name === route.name) index = state.visitedRoutes.indexOf(item)
      return item.affix || index >= state.visitedRoutes.indexOf(item)
    })
  },
  //删除全部标签
  delAllVisitedRoutes(state) {
    state.visitedRoutes = state.visitedRoutes.filter(
      (item) => item.affix == true
    )
  },
}
const actions = {
  //添加标签
  addVisitedRoute({ commit }, route) {
    commit('addVisitedRoute', route)
  },
  //删除当前标签
  delVisitedRoute({ commit }, route) {
    commit('delVisitedRoute', route)
  },
  //删除当前标签以外其它全部多标签
  delOthersVisitedRoutes({ commit }, route) {
    commit('delOthersVisitedRoutes', route)
  },
  //删除当前标签左边全部标签
  delLeftVisitedRoutes({ commit }, route) {
    commit('delLeftVisitedRoutes', route)
  },
  //删除当前标签右边全部标签
  delRightVisitedRoutes({ commit }, route) {
    commit('delRightVisitedRoutes', route)
  },
  //删除全部标签
  delAllVisitedRoutes({ commit }) {
    commit('delAllVisitedRoutes')
  },
}
export default { state, getters, mutations, actions }
